{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python实战-32个常用入门"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.冒泡排序\n",
    "\n",
    "- 冒泡排序（Bubble Sort），是一种计算机科学领域的较简单基础的排序算法。其基本思路是，对于一组要排序的元素列，依次比较相邻的两个数，将比较小的数放在前面，比较大的数放在后面，如此继续，直到比较到最后的两个数，将小数放在前面，大数放在后面，重复步骤，直至全部排序完成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lis = [78,122,1,8,354,10,100,34,46,-37,27,456,294,-5]\n",
    "len(lis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 13)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(len(lis)-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-37, -5, 1, 8, 10, 27, 34, 46, 78, 100, 122, 294, 354, 456]\n"
     ]
    }
   ],
   "source": [
    "def sortport():\n",
    "    for i in range(len(lis)-1):\n",
    "        for j in range(len(lis)-1-i):\n",
    "            if lis[j]>lis[j+1]:\n",
    "                lis[j],lis[j+1] = lis[j+1],lis[j]\n",
    "    return lis\n",
    "if __name__ == '__main__':\n",
    "    sortport()\n",
    "    print(lis)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.计算x的n次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def power(x, n):\n",
    "    s = 1\n",
    "    while n > 0:\n",
    "        n = n - 1\n",
    "        s = s * x\n",
    "    return s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 调用方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "power(2,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.计算阶乘 n!\n",
    "### 方法一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个数字：5\n",
      "5的阶乘为120\n"
     ]
    }
   ],
   "source": [
    "#方法一\n",
    "def fac():\n",
    "    num = int(input(\"请输入一个数字：\"))\n",
    "    factorial = 1\n",
    "    #查看数字是负数，0或者正数\n",
    "    if num<0:\n",
    "        print(\"抱歉，负数没有阶乘\")\n",
    "    elif num == 0:\n",
    "        print(\"0的阶乘为1\")\n",
    "    else:\n",
    "        for i in range(1,num+1):\n",
    "            factorial = factorial*i\n",
    "        print(\"%d的阶乘为%d\"%(num,factorial))\n",
    "if __name__ == '__main__':\n",
    "    fac()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 说明 if __name__ == '__main__': 这句代码的作用：\n",
    "- 一个python文件通常有两种使用方法，第一是作为脚本直接执行，第二是 import 到其他的 python 脚本中被调用（模块重用）执行。因此 if __name__ == 'main': 的作用就是控制这两种情况执行代码的过程，在 if __name__ == 'main': 下的代码只有在第一种情况下（即文件作为脚本直接执行）才会被执行，而 import 到其他脚本中是不会被执行的。\n",
    "#### 总的来说，这句代码的作用就是既能保证当前的.py文件直接运行，也能保证其可以作为模块被其他.py文件导入。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方法二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个数字：6\n",
      "6的阶乘为720\n"
     ]
    }
   ],
   "source": [
    "#方法二\n",
    "def fac():\n",
    "    num = int(input(\"请输入一个数字：\"))\n",
    "    #查看数字是负数，0或者正数\n",
    "    if num<0:\n",
    "        print(\"抱歉，负数没有阶乘\")\n",
    "    elif num == 0:\n",
    "        print(\"0的阶乘为1\")\n",
    "    else:\n",
    "        print(\"%d的阶乘为%d\"%(num,factorial(num)))\n",
    "def factorial(n):\n",
    "    result = n\n",
    "    for i in range(1,n):\n",
    "        result=result*i\n",
    "    return result\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    fac()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方法三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个数字：7\n",
      "7的阶乘为5040\n"
     ]
    }
   ],
   "source": [
    "#方法三\n",
    "def fac():\n",
    "    num = int(input(\"请输入一个数字：\"))\n",
    "    #查看数字是负数，0或者正数\n",
    "    if num<0:\n",
    "        print(\"抱歉，负数没有阶乘\")\n",
    "    elif num == 0:\n",
    "        print(\"0的阶乘为1\")\n",
    "    else:\n",
    "        print(\"%d的阶乘为%d\"%(num,fact(num)))\n",
    "\n",
    "def fact(n):\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    return n * fact(n - 1)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    fac()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.计算a * a + b * b + c * c + …"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n"
     ]
    }
   ],
   "source": [
    "def calc(*numbers):\n",
    "    sum=0\n",
    "    for n in numbers:\n",
    "        sum=sum+n*n\n",
    "    return sum\n",
    "print(calc(3,4,5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.把一个list中所有的字符串变成小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "world\n",
      "ibm\n",
      "apple\n",
      "taobao\n",
      "baidu\n"
     ]
    }
   ],
   "source": [
    "L = ['Hello','World','IBM','Apple','TAOBao','BaiDu']\n",
    "\n",
    "for s in L:\n",
    "    s=s.lower()\n",
    "    print(s)     #将list中每个字符串都变成小写，返回每个字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.列出当前目录下的所有文件和目录名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "for d in os.listdir('.'):\n",
    "    print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.输出某个路径下的所有文件和文件夹的路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_dir():\n",
    "  filepath = input(\"请输入一个路径：\")\n",
    "  if filepath == \"\":\n",
    "    print(\"请输入正确的路径\")\n",
    "  else:\n",
    "    for i in os.listdir(filepath):       #获取目录中的文件及子目录列表\n",
    "      print(os.path.join(filepath,i))  #把路径组合起来\n",
    "\n",
    "print(print_dir())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.输出某个路径及其子目录下的所有文件路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_dir(filepath):\n",
    "  for i in os.listdir(filepath):\n",
    "    path = (os.path.join(filepath, i))\n",
    "    print(path)\n",
    "    if os.path.isdir(path):      #isdir()判断是否是目录\n",
    "      show_dir(path)             #如果是目录，使用递归方法\n",
    "\n",
    "filepath = \"C:\\Program Files\\Internet Explorer\"\n",
    "show_dir(filepath)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
